package com.ithema.edu.info.manager.controller;

import com.ithema.edu.info.manager.domain.Student;
import com.ithema.edu.info.manager.service.StudentService;

import java.util.Scanner;

public  class BaseStudentController {
    private Scanner sc = new Scanner(System.in);
    private StudentService se = new StudentService();

    public void start() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:
                    addStudents();  //添加学生
                    break;
                case 2:
                    deleteStudent();  //删除学生
                    break;
                case 3:
                    updateStudent();  //修改学生
                    break;
                case 4:
                    //queryStudents();  //查看学生
                    break;
                case 5:
                    System.out.println("退出学生管理系统");
                    return;//将方法结束!!!!
                default:
                    System.out.println("您输入的有误！请重新输入！");
                    break;
            }
        }
    }


    //.......................................
    /* 添加学生*/
    //.......................................

    public void addStudents() {

        System.out.println("请输入您要添加的学生学号：");
        String id = sc.next();
        System.out.println("请输入您要添加的学生姓名：");
        String name = sc.next();
        System.out.println("请输入您要添加的学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入您要添加的学生手机号：");
        String phone = sc.next();
        Student student = new Student(id, name, age, phone);

        se.addStudents(student);
        System.out.println("添加成功！");

    }

    //.......................................
    /*删除学生*/
    //.......................................
    public void deleteStudent() {


        while (true) {
            System.out.println("请输入要删除的学生id");
            String id = sc.next();
            if ("over".equals(id)) {
                System.out.println("感谢你的使用！");
                return;
            }

            boolean b = se.deleteStudent(id);
            if (b) {
                System.out.println("删除成功！");
                break;
            } else {
                System.out.println("您输入的id不存在！请重新输入！若想退出请输入over!");

            }
        }

    }

    //.......................................
    /* 修改学生*/
    //.......................................
    public void updateStudent() {

        String id;
        int index;
        while (true) {
            System.out.println("请输入您要修改的学生学号：");
            id = sc.next();
            if ("over".equals(id)) {
                return;
            }
            index = se.isExists(id);
            if (index == -1) {
                System.out.println("您输入的id不存在，请重新输入！若退出请输入over!");
            } else {
                break;
            }
        }

        System.out.println("请输入新的学生姓名：");
        String name = sc.next();
        System.out.println("请输入新的学生年龄：");
        int age = sc.nextInt();
        System.out.println("请输入新的学生手机号：");
        String phone = sc.next();
        Student newstudent = new Student(id, name, age, phone);
        se.updateStudent(index, newstudent);
        System.out.println("修改成功！");

    }

    //.......................................
    /* 查看学生*/
    //.......................................

}
